package cn.kys.modules.system.controller;

import cn.kys.modules.system.service.FormContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/formcontent_api")
public class FormContentApi {
    @Autowired
    private FormContentService formContentService;

    @GetMapping("/getAllFormcontents")
    public Map getAllFormcontents() {
        Map returnMap = new HashMap();
        try {
            List list = formContentService.getAllFormcontents();
            returnMap.put("data", list);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }

    @GetMapping("/getAllContentTypes")
    public Map getAllContentTypes() {
        Map returnMap = new HashMap();
        try {
            List list = formContentService.getAllContentTypes();
            returnMap.put("data", list);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }

    //添加表单内容项（多个）
    @GetMapping("/addMultiFormContents")
    public Map addMultiFormContents(@RequestParam("jsonStr") String jsonStr) {
        Map returnMap = new HashMap();
        try {
            formContentService.addMultiFormContents(jsonStr);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }

    //添加表单内容项
    @GetMapping("/addFormContent")
    public Map addFormContent(@RequestParam("name") String name, @RequestParam("cname") String cname, @RequestParam("type") String type, @RequestParam("enum") String enumvalue) {
        Map returnMap = new HashMap();
        try {
            Map map = formContentService.addFormContent(name, cname, type, enumvalue);
            returnMap.put("data", map);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }

    //编辑表单内容项
    @GetMapping("/editFormContent")
    public Map editFormContent(@RequestParam("id") String id, @RequestParam("name") String name, @RequestParam("cname") String cname, @RequestParam("type") String type, @RequestParam("enum") String enumvalue) {
        Map returnMap = new HashMap();
        try {
            Map map = formContentService.editFormContent(id, name, cname, type, enumvalue);
            returnMap.put("data", map);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }


    //导入表单内容excel
    @PostMapping("/importFormContentsByExcel")
    public Map importFormContentsByExcel(@RequestParam("file") MultipartFile file, @RequestParam("layerid") int layerid) {
        Map returnMap = new HashMap();
        try {
            Map map = formContentService.importFormContentsByExcel(file, layerid);
            returnMap.put("data", map);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }

    @GetMapping("/delFormContentById")
    public Map delFormContentById(@RequestParam("id") String id) {
        Map returnMap = new HashMap();
        try {
            Map map = formContentService.delFormContentById(id);
            returnMap.put("data", map);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }
}

